package com.supreme.studyserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.supreme.commonboot.utils.GlobalSpringUtils;
import com.supreme.commonboot.utils.ReturnEntity;
import com.supreme.commonrocketmq.annotation.LoggerRecord;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.studyserver.component.QumApplicationContext;
import com.supreme.studyserver.config.props.StudyServiceProps;
import com.supreme.studyserver.entity.Student;
import com.supreme.studyserver.entity.Test;
import com.supreme.studyserver.service.MdsService;
import com.supreme.studyserver.service.TestService;
import com.supreme.studyserver.strategy.FullStrategyService;
import com.supreme.studyserver.strategy.StrategyFactory;
import com.supreme.studyserver.strategy.StrategyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;
import org.supreme.starter.tools.properties.BasicProperties;
import org.supreme.starter.tools.properties.ToolsProperties;
import org.supreme.starter.tools.service.HelloWorld;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

@LoggerRecord
@Slf4j
@RestController
@RequestMapping(value = "test")
public class TestController {

    @Autowired
    @Qualifier(value = "chrisStudent")
    private Student student;

    @Resource
    private HelloWorld helloWorld;

    @Resource
    private ToolsProperties toolsProperties;

    @Resource
    private StudyServiceProps studyServiceProps;

    @Resource
    private BasicProperties basicProperties;

    @Resource
    private TestService testService;

    @Resource
    private StrategyFactory strategyFactory;

    @Resource
    private HttpServletRequest httpServletRequest;

    @Value("${SERVER_NODE_INDEX}")
    private String serverNodeIndex;

    @Value("${TEMP_ENV}")
    private String tempEnv;

    @GetMapping(value = "hello")
    public AjaxResp hello() {
        System.out.println(student);
        return AjaxResp.instance(student);
    }

    @GetMapping(value = "world")
    public AjaxResp world() {
        return AjaxResp.instance(
                new JSONObject()
                        .fluentPut("serverNodeIndex", this.serverNodeIndex)
                        .fluentPut("testEnv", this.tempEnv));
    }

    @GetMapping(value = "fileBeat/{level}")
    public AjaxResp fileBeat(@PathVariable(value = "level") Integer level) {
        String randomAlphabetic = RandomStringUtils.randomAlphabetic(16);
        switch (level) {
            case -1:
                break;

            case 0:
                log.debug(randomAlphabetic);
                log.info(randomAlphabetic);
                log.warn(randomAlphabetic);
                log.error(randomAlphabetic);
                break;

            case 1:
                log.debug(randomAlphabetic);
                break;

            case 2:
                log.info(randomAlphabetic);
                break;

            case 3:
                log.warn(randomAlphabetic);
                break;

            case 4:
                log.error(randomAlphabetic);
                break;
        }

        return AjaxResp.instance(randomAlphabetic);
    }

    @GetMapping(value = "logger")
    public AjaxResp logger(@RequestParam(value = "message") String message) {
        log.error("logger error = {}", message);
        log.warn("logger warn = {}", message);
        log.info("logger info = {}", message);
        log.debug("logger debug = {}", message);
        log.trace("trace trace = {}", message);
        return AjaxResp.instance();
    }

    @PostMapping(value = "jvm/{key}")
    public AjaxResp jvmInfo(@PathVariable(value = "key") String key,
                            @RequestParam(value = "itemKey") String itemKey,
                            @RequestBody Test test) {
        final JSONObject jsonObject = new JSONObject() {{
            put("pathKey", key);
            put("itemKey", itemKey);
            put("testData", test);
        }};
        return AjaxResp.instance(jsonObject);
    }

    @GetMapping(value = "computer")
    public AjaxResp computer() {
        Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            System.out.println("name = " + name);
        }
        return AjaxResp.instance();
    }

    @PostMapping(value = "getTest")
    public AjaxResp getTest(@RequestBody Test test) {
        log.debug("getTest1: {}", test.toString());
        log.debug("getTest2: {}", test.toString());
        testService.testShow(test);
        return AjaxResp.instance(test);
    }

    @PostMapping(value = "testReturn")
    public AjaxResp testReturn(@RequestBody Test test) {
        ReturnEntity<Test> testReturnEntity = testService.testReturn(test);
        return AjaxResp.instance(testReturnEntity.getData());
    }

    @PostMapping(value = "appStop")
    public AjaxResp appStop() {
        QumApplicationContext.applicationStop();
        return AjaxResp.instance();
    }

    @GetMapping(value = "testStarter")
    public AjaxResp testStarter() {
        final JSONObject jsonObject = new JSONObject()
                .fluentPut("title", toolsProperties.getTitle())
                .fluentPut("username", toolsProperties.getUsername())
                .fluentPut("password", toolsProperties.getPassword())
                .fluentPut("host", basicProperties.getHost())
                .fluentPut("port", basicProperties.getPort())
                .fluentPut("method", basicProperties.getMethod())
                .fluentPut("path", basicProperties.getPath())
                .fluentPut("version", basicProperties.getVersion())
                .fluentPut("title", helloWorld.getTitle())
                .fluentPut("content", helloWorld.getContent())
                .fluentPut("serverName", studyServiceProps.getServerName())
                .fluentPut("serverCondition", studyServiceProps.getServerCondition())
                .fluentPut("status", 0);
        return AjaxResp.instance(jsonObject);
    }

    @GetMapping(value = "strategyFactory/{strategyCode}")
    public AjaxResp strategyFactory(@PathVariable(value = "strategyCode") String strategyCode) {
        StrategyService strategy = strategyFactory.getStrategy(strategyCode);
        strategy.showStrategyInfo();
        return AjaxResp.instance();
    }

    @GetMapping(value = "normalMultiDataSource/{name}/{type}")
    public AjaxResp normalMultiDataSource(@PathVariable(value = "name") String name,
                                          @PathVariable(value = "type") Integer type) {
        MdsService mdsService = GlobalSpringUtils.getBean(MdsService.class);
        mdsService.normalMultiDataSource(name, type);
        return AjaxResp.instance();
    }

    @GetMapping(value = "transactionMultiDataSource/{name}/{type}")
    public AjaxResp transactionMultiDataSource(@PathVariable(value = "name") String name,
                                               @PathVariable(value = "type") Integer type) {
        MdsService mdsService = GlobalSpringUtils.getBean(MdsService.class);
        mdsService.transactionMultiDataSource(name, type);
        return AjaxResp.instance();
    }
}
